from fastapi import APIRouter
from fastapi.responses import JSONResponse, Response, FileResponse
from .file_manager import FileManager
from .models import *

router = APIRouter(tags=['File Download'])
file_manager = FileManager()


@router.get('/download/files', response_class=JSONResponse, response_model=list[FileInfo])
def get_file_list(refresh:bool=False):
    if refresh:
        file_manager.refresh_list()
    return file_manager.get_file_list()


@router.get('/download/files/{fid}')
def get_file(fid: int):
    return FileResponse(file_manager.get_file_path(fid))


@router.delete('/download/files/all',response_class=JSONResponse,response_model=list[FileInfo])
def remove_all():
    file_manager.remove_all()
    return file_manager.get_file_list()

@router.delete('/download/files/{fid}', response_class=JSONResponse, response_model=list[FileInfo])
def remove_file(fid: int):
    file_manager.remove_file(fid)
    return file_manager.get_file_list()


# @router.on_event('shutdown')
# def close_router():
#     print('Downlod router cloased')
#     file_manager.save_file_dict()
